package org.linlinjava.litemall.admin.web.order_manage;

import org.linlinjava.litemall.admin.constant.HeaderConstant;
import org.linlinjava.litemall.admin.form.StoreStaffAddForm;
import org.linlinjava.litemall.admin.service.order_manage.AgedmallShopStaffService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/shop/staffManage")
public class AgedmallShopStaffController {

    @Autowired
    private AgedmallShopStaffService agedmallShopStaffService;

    /**
     * 员工列表
     */
    @PostMapping("/list")
    public Object list(@RequestParam(required = false) String keySearch,
                       @RequestParam(required = false) Integer storeId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallShopStaffService.list(busiId, keySearch, storeId, page, limit);
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    public Object add(@RequestBody @Validated StoreStaffAddForm storeStaffAddForm, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallShopStaffService.add(busiId, storeStaffAddForm);
    }

    /**
     * 编辑用户
     */
    @PostMapping("/update")
    public Object update(@RequestBody StoreStaffAddForm storeStaffAddForm, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallShopStaffService.update(busiId, storeStaffAddForm);
    }

    /**
     * 员工管理-删除
     */
    @PostMapping("/del")
    public Object del(@RequestParam Integer staffId){
        return agedmallShopStaffService.del(staffId);
    }

    /**
     * 员工管理-员工详情-获取订单列表
     */
    @PostMapping("/detailOrderList")
    public Object detailOrderList(@RequestParam Integer staffId,
                                  @RequestParam(required = false) Integer state,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallShopStaffService.detailOrderList(busiId, staffId, state, page, limit);
    }
}
